﻿<UserControl x:Class=" LocultApp.Views.Pages.EditPageDocuments.EditTranslationDocumentView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             
             xmlns:conv="clr-namespace:LocultApp.Converter"
             xmlns:behav="clr-namespace:LocultApp.Behaviours"
             xmlns:proc="clr-namespace:MSTranslate.Interfaces;assembly=MSTranslate"
             xmlns:views="clr-namespace:LocultApp.Views.Pages.EditPageDocuments"
             xmlns:local="clr-namespace:LocultApp.Local"
             
             xmlns:System="clr:namespace:System;assembly=mscorlib"
             
             mc:Ignorable="d" 
             d:DesignHeight="400" d:DesignWidth="500">
    <Grid>
        <Grid.Resources>
            <conv:InverseBooleanConverter x:Key="ConvertInverseBool" />
            <conv:AlternateColorConverter x:Key="ConvertBoolToTransparent" />
        </Grid.Resources>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <Grid Grid.Row="0"
              IsEnabled="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=views:EditTranslationDocumentView,AncestorLevel=1}, Path=IsEnablePage}">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>

            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>

            <TextBlock Grid.Row="0" Style="{StaticResource Heading1}" Margin="0,0,0,12">
                <TextBlock Text="{Binding Source={x:Static local:Strings.STR_Translate}, StringFormat={}{0}\:}"  Style="{StaticResource Heading1}" />
                <TextBlock Text="{Binding StringDiff.SourceFileName}"
                           ToolTip="{Binding StringDiff.SourceFilePath}" Style="{StaticResource Heading1}" />
                <TextBlock Text="->" Style="{StaticResource Heading1}" />
                <TextBlock ToolTip="{Binding StringDiff.TargetFilePath}">
                    <TextBlock.Style>
                        <Style TargetType="TextBlock">
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding Path=IsDirty, UpdateSourceTrigger=PropertyChanged}" Value="False">
                                    <Setter Property="Text" Value="{Binding StringDiff.TargetFileName, UpdateSourceTrigger=PropertyChanged}"/>
                                </DataTrigger>
                                <DataTrigger Binding="{Binding Path=IsDirty, UpdateSourceTrigger=PropertyChanged}" Value="True">
                                    <Setter Property="Text" Value="{Binding StringDiff.TargetFileName, StringFormat='\{0\}*', UpdateSourceTrigger=PropertyChanged}"/>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </TextBlock.Style>
                </TextBlock>
            </TextBlock>

            <Button Grid.Column="0" Grid.Row="1" Content="{x:Static local:Strings.STR_CAPTION_SAVE}"
                    Command="{Binding SaveTargetCommand}"
                    CommandParameter="{Binding StringDiff.TargetFilePath}"
                    HorizontalAlignment="Left" VerticalAlignment="Center" Margin="3" Padding="3,0,3,3" MinWidth="25"/>
        </Grid>

        <Grid Grid.Row="1">
            <Grid.Resources>
                <conv:BoolToVisibilityConverter x:Key="BoolToHiddenConverter" TrueValue="Visible" FalseValue="Hidden" />
            </Grid.Resources>

            <Grid Visibility="{Binding StringDiff.ProcessAction.IsProcessing, Converter={StaticResource BoolToHiddenConverter}}">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>

                <ProgressBar Grid.Column="0" IsIndeterminate="True" Height="10" Margin="3" />
                <Button Grid.Column="1"
                        Content="{x:Static local:Strings.STR_CAPTION_CANCEL}"
                        Command="{Binding StringDiff.CancelTranslationCommand}" Margin="3"/>
            </Grid>
        </Grid>

        <!--
            CanUserAddRows="False" <- Disables adding row feature in DataGrid View!
            AlternatingRowBackground Controls whether every 2nd row is colored or not.
        -->
        <DataGrid Grid.Row="2"
                  Grid.Column="0"
                  Margin="3,1,0,0"
                  CanUserAddRows="False" 
                  
                  IsSynchronizedWithCurrentItem="True"
                  
                  MinHeight="10"
                  SelectedItem="{Binding Path=StringDiff.DiffSource.SelectedItem}"

                  ItemsSource="{Binding Path=StringDiff.DiffSource.LogView}"
                  AutoGenerateColumns="False"
                  RowHeaderWidth="40"

                  SelectionMode="Extended"
                  behav:DataGridMultipleSelection.SelectedItemsSource="{Binding StringDiff.DiffSource.SelectedItems}"
                  behav:SelectedItem.IsBroughtIntoViewWhenSelected="True"
                  ScrollViewer.CanContentScroll="True"
                  ScrollViewer.HorizontalScrollBarVisibility="Auto"
                  ScrollViewer.VerticalScrollBarVisibility="Auto"
                  
                  Name="PART_Grid"
                  IsEnabled="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=views:EditTranslationDocumentView,AncestorLevel=1}, Path=IsEnablePage}"
                  >
            <DataGrid.AlternatingRowBackground>
                <MultiBinding Converter="{StaticResource ConvertBoolToTransparent}">
                    <Binding Path="ColorGridOption"/>
                    <Binding Path="AlternateGridColor" />
                </MultiBinding>
            </DataGrid.AlternatingRowBackground>
            <!--  AlternateGridColor
                  AlternatingRowBackground="{Binding ColorGridOption, Converter={StaticResource ConvertBoolToTransparent}, ConverterParameter=AlternateGridColor}"
            -->
            <DataGrid.ContextMenu>
                <ContextMenu>
                    <!-- Delete Command is disabled since deleting/inserting rows is not allowed in current release -->
                    <!-- MenuItem Header="Delete" Command="{Binding DeleteSelectedEntries}" / -->
                    <MenuItem Header="{x:Static local:Strings.STR_Translate_ContextMenu_Caption}" Command="{Binding StringDiff.TranslateCommand}" />
                </ContextMenu>
            </DataGrid.ContextMenu>
            <DataGrid.Columns>

                <DataGridTemplateColumn Header="{x:Static local:Strings.STR_State}" Width="SizeToCells"
                                        MinWidth="50"
                                        CanUserSort="True" SortMemberPath="Difference">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Image Source="{Binding Path=Difference,Converter={conv:TypeOfDiffToImageConverter}, UpdateSourceTrigger=PropertyChanged}" Width="16"
                                   ToolTip="{Binding Path=Difference,Converter={conv:TypeOfDiffToStringConverter}}"
                                   VerticalAlignment="Top" HorizontalAlignment="Left" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>

                <!-- Key column is readonly since users should not edit this column at any time -->
                <DataGridTextColumn Header="{x:Static local:Strings.STR_KEY_GRIDCOLUMN_HEADER}" Width="SizeToCells" IsReadOnly="True"
                                    CanUserSort="True" SortMemberPath="Key"
                                    Binding="{Binding Key, TargetNullValue=(enter a key string that has no space)}"
                                    ClipboardContentBinding="{Binding Key}" >
                    <DataGridTextColumn.CellStyle>
                        <Style TargetType="{x:Type DataGridCell}" />
                        <!-- BasedOn="{StaticResource baseStyle}" -->
                    </DataGridTextColumn.CellStyle>
                </DataGridTextColumn>

                <!-- Source language column is readonly since users should not edit this column at any time -->
                <DataGridTextColumn Header="{x:Static local:Strings.STR_SourceLanguage_GridColumn_Header}"
                                    MinWidth="100"
                                    Width="*" IsReadOnly="True"
                            CanUserSort="True" SortMemberPath="SourceValue"
                            Binding="{Binding SourceValue}" ClipboardContentBinding="{Binding SourceValue}" >
                    <DataGridTextColumn.CellStyle>
                        <Style TargetType="{x:Type DataGridCell}" />
                        <!-- BasedOn="{StaticResource baseStyle}" -->
                    </DataGridTextColumn.CellStyle>
                    <DataGridTextColumn.ElementStyle>
                        <Style TargetType="TextBlock">
                            <Setter Property="TextWrapping" Value="Wrap" />
                            <Setter Property="ToolTip" Value="{Binding SourceComment, Converter={conv:EmptyStringToNullConverter}}" />
                            <Setter Property="ToolTipService.ShowOnDisabled" Value="True" />
                        </Style>
                    </DataGridTextColumn.ElementStyle>
                    <DataGridTextColumn.EditingElementStyle>
                        <Style TargetType="TextBox">
                            <Setter Property="TextWrapping" Value="Wrap" />
                            <Setter Property="AcceptsReturn" Value="true" />
                        </Style>
                    </DataGridTextColumn.EditingElementStyle>
                </DataGridTextColumn>

                <!-- Target language column should be editable to give user a chance to enter a translation -->
                <DataGridTextColumn Header="{x:Static local:Strings.STR_TargetLanguage_GridColumn_Header}" IsReadOnly="False"
                                    CanUserSort="True" SortMemberPath="TargetValue"
                                    Binding="{Binding TargetValue}"
                                    ClipboardContentBinding="{Binding TargetValue}"
                                    MinWidth="100"
                                    Width="*">
                    <DataGridTextColumn.CellStyle>
                        <Style TargetType="{x:Type DataGridCell}" />
                        <!-- BasedOn="{StaticResource baseStyle}"  -->
                    </DataGridTextColumn.CellStyle>
                    <DataGridTextColumn.ElementStyle>
                        <Style TargetType="TextBlock">
                            <Setter Property="TextWrapping" Value="Wrap" />
                            <Setter Property="ToolTip" Value="{Binding TargetComment, Converter={conv:EmptyStringToNullConverter}}" />
                            <Setter Property="ToolTipService.ShowOnDisabled" Value="True" />
                        </Style>
                    </DataGridTextColumn.ElementStyle>
                    <DataGridTextColumn.EditingElementStyle>
                        <Style TargetType="TextBox">
                            <Setter Property="TextWrapping" Value="Wrap" />
                            <Setter Property="AcceptsReturn" Value="true" />
                        </Style>
                    </DataGridTextColumn.EditingElementStyle>
                </DataGridTextColumn>
            </DataGrid.Columns>
        </DataGrid>

        <GridSplitter Grid.Row="3" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Height="3" />

        <Grid Grid.Row="4" Margin="3"
              IsEnabled="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=views:EditTranslationDocumentView,AncestorLevel=1}, Path=IsEnablePage}">
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>

            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>

            <Grid Grid.Row="0" Grid.Column="0">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="*" />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>

                <StackPanel Grid.Row="1">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>

                        <Label Grid.Column="0"
                               Content="{Binding Path=StringDiff.DiffSource.SelectedItemBuffer.SourceKey,Mode=OneWay,FallbackValue=' '}"
                               Margin="3,0,3,0"
                               VerticalAlignment="Bottom" HorizontalAlignment="Left"/>

                        <ComboBox Grid.Column="1"
                                  ItemsSource="{Binding StringDiff.LanguageList}"
                                  SelectedItem="{Binding StringDiff.SelectedSourceLanguage}"
                                  SelectedValuePath="Bcp47_LangCode"
                                  HorizontalAlignment="Right" VerticalAlignment="Bottom"
                                  MinWidth="150"
                                  Margin="3,3,0,3"
                                  ItemTemplate = "{StaticResource LanguageDataTemplate}"
                                />
                    </Grid>
                    <TextBox Text="{Binding Path=StringDiff.DiffSource.SelectedItemBuffer.SourceValue,Mode=OneWay}"
                             TextWrapping="WrapWithOverflow"
                             VerticalAlignment="Stretch"
                             MinHeight="75" Margin="3"
                             Name="Part_SourceTextBox"
                             ToolTip="Source Language String"
                             ToolTipService.ShowOnDisabled="True"
                         
                             IsEnabled="False"
                             IsReadOnly="True"
                    />
                    <!-- IsEnabled="{Binding ElementName=PART_Grid, Path=SelectedItems.Count,Converter={conv:SelectItemCountToBooleanConverter},UpdateSourceTrigger=PropertyChanged}" -->
                    <!-- This is not editable since only target column should be editable -->
                </StackPanel>

                <StackPanel Grid.Row="2">
                    <Label Content="{x:Static local:Strings.STR_LABEL_Comment}" Margin="0,3,0,3" />
                    <!--
                 IsEnabled="{Binding ElementName=PART_Grid, Path=SelectedItems.Count,Converter={conv:SelectItemCountToBooleanConverter},UpdateSourceTrigger=PropertyChanged}"
                -->
                    <TextBox Text="{Binding Path=StringDiff.DiffSource.SelectedItemBuffer.SourceComment,Mode=OneWay}"
                             TextWrapping="WrapWithOverflow"
                             VerticalAlignment="Stretch"
                             MinHeight="75" Margin="3"
                             Name="Part_SourceCommentTextBox"
                             ToolTip="Source Comment String - Use this field to store additional comments that are helpful for translators."
                             ToolTipService.ShowOnDisabled="True"
                         
                             IsEnabled="False"
                             IsReadOnly="True"
                    />
                    <!-- This is not editable since only target column should be editable -->

                    <!-- the IsEnabled Converter enables the control only if there is only one row selected -->
                </StackPanel>
            </Grid>

            <StackPanel Orientation="Vertical" Grid.Row="1"  Grid.Column="1">
                <!--
                IsEnabled="{Binding Path=StringDiff.SelectedItems.Count,Converter={conv:SelectItemCountToBooleanConverter},UpdateSourceTrigger=PropertyChanged}"
                -->
                <Button Content="{x:Static local:Strings.STR_APPLY_BUTTON_CAPTION}"
                        Command="{Binding ApplyCurrentEdit}" Margin="3">
                    <Button.CommandParameter>
                        <MultiBinding Converter="{conv:MultiParametersConverter}">
                            <Binding ElementName="Part_SourceTextBox" Path="Text"/>
                            <Binding ElementName="Part_SourceCommentTextBox" Path="Text" />
                            <Binding ElementName="Part_TargetTextBox" Path="Text"/>
                            <Binding ElementName="Part_TargetCommentTextBox" Path="Text" />
                        </MultiBinding>
                    </Button.CommandParameter>
                </Button>

                <!-- Translate from left to right -->
                <Button Content="{x:Static local:Strings.STR_Translate_FromTargetTo_Source}"
                        ContentStringFormat="{}{0} &gt;"
                        HorizontalAlignment="Left" VerticalAlignment="Bottom"
                        Command="{Binding StringDiff.TranslateCommand}"
                        CommandParameter="{x:Static proc:ProcessDestination.Target}"
                        Margin="3">
                </Button>

                <!-- Translate from right to left 
                Writing to source document is more an experimental thing than anything else,
                its not a required use case at his time.
                <Button Content="{x:Static local:Strings.STR_Translate_FromSourceTo_Target}"
                        ContentStringFormat="{}&lt; {0}"
                        HorizontalAlignment="Left" VerticalAlignment="Top"
                        Command="{Binding StringDiff.TranslateCommand}"
                        CommandParameter="{x:Static proc:ProcessDestination.Source}"
                        Visibility="Collapsed"
                        Margin="3">
                </Button>
                -->
            </StackPanel>

            <Grid Grid.Row="2"  Grid.Column="2">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="*" />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>

                <Grid Grid.Row="0">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>

                    <ComboBox Grid.Column="0"
                              ItemsSource="{Binding StringDiff.LanguageList}"
                              SelectedItem="{Binding StringDiff.SelectedTargetLanguage}"
                              SelectedValuePath="Bcp47_LangCode"
                              HorizontalAlignment="Left" VerticalAlignment="Bottom"
                              MinWidth="150"
                              Margin="3,3,0,3"
                              ItemTemplate = "{StaticResource LanguageDataTemplate}"
                              />

                    <Label Grid.Column="1" HorizontalAlignment="Right" VerticalAlignment="Bottom"
                           Content="{Binding Path=StringDiff.DiffSource.SelectedItemBuffer.TargetKey,Mode=OneWay}"
                           Margin="3,0,3,0" />
                </Grid>

                <StackPanel Grid.Row="1">
                    <TextBox Text="{Binding Path=StringDiff.DiffSource.SelectedItemBuffer.TargetValue,Mode=OneWay}"
                             TextWrapping="WrapWithOverflow"
                             VerticalAlignment="Stretch"
                             MinHeight="75"
                             Margin="3"
                             IsEnabled="{Binding ElementName=PART_Grid, Path=SelectedItems.Count,Converter={conv:SelectItemCountToBooleanConverter},UpdateSourceTrigger=PropertyChanged}"
                             Name="Part_TargetTextBox"
                             ToolTip="Target Language String"
                             ToolTipService.ShowOnDisabled="True"
                    />
                </StackPanel>

                <StackPanel Grid.Row="2">
                    <Label Content="{x:Static local:Strings.STR_LABEL_Comment}"
                           ContentStringFormat="{}{0}:"
                           Margin="3" />
                    <TextBox Text="{Binding Path=StringDiff.DiffSource.SelectedItemBuffer.TargetComment,Mode=OneWay}"
                            TextWrapping="WrapWithOverflow"
                            VerticalAlignment="Stretch"
                            MinHeight="75" Margin="3"
                            IsEnabled="{Binding ElementName=PART_Grid, Path=SelectedItems.Count,Converter={conv:SelectItemCountToBooleanConverter},UpdateSourceTrigger=PropertyChanged}"
                            Name="Part_TargetCommentTextBox"
                    />
                </StackPanel>
                <!-- the IsEnabled Converter enables the control only if there is only one row selected -->
            </Grid>

        </Grid>

    </Grid>
</UserControl>
